package gepes.dao;

import gepes.exepcion.DAOExcepcion;
import gepes.modelo.Estudiante;
import gepes.modelo.Profesor;
import gepes.modelo.SubCategoria;
import gepes.modelo.Usuario;
import gepes.util.ConexionBD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

public class ProfesorDAO extends BaseDAO {
	//
	public int existe(String campo, String dato) throws DAOExcepcion {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select count(0) from profesor where " + campo
					+ "=?";

			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, dato);
			rs = stmt.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return 0;

	}

	public Profesor login(Profesor us) throws DAOExcepcion {
		String query = "select nombres,apepaterno,apematerno,sexo,tipodocumento,nrodocumento,"
				+ "correo,celular,"
				+ " clave,perfil,codprofesor,foto from profesor where "
				+ " correo=? and clave=?";
		Connection con = null;
		PreparedStatement stmt = null;
		Profesor vo = new Profesor();
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, us.getCorreo());
			stmt.setString(2, us.getClave());
			rs = stmt.executeQuery();
			if (rs.next()) {

				vo.setNombres(rs.getString(1));
				vo.setApepaterno(rs.getString(2));
				vo.setApematerno(rs.getString(3));
				vo.setSexo(rs.getString(4));
				vo.setTipodocumento(rs.getString(5));
				vo.setNrodocumento(rs.getString(6));
				vo.setCorreo(rs.getString(7));
				vo.setCelular(rs.getString(8));
				vo.setClave(rs.getString(9));
				vo.setPerfil(rs.getString(10));
				vo.setCodprofesor(rs.getInt(11));
				vo.setFoto(rs.getString(12));
				return vo;
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	public Profesor obtener(int id) throws DAOExcepcion {
		String query = "select nombres,apepaterno,apematerno,sexo,tipodocumento,nrodocumento,"
				+ "correo,celular,"
				+ " clave,perfil,codprofesor,foto from profesor where "
				+ " codprofesor=?";
		Connection con = null;
		PreparedStatement stmt = null;
		Profesor vo = new Profesor();
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setInt(1,id);
			rs = stmt.executeQuery();
			if (rs.next()) {

				vo.setNombres(rs.getString(1));
				vo.setApepaterno(rs.getString(2));
				vo.setApematerno(rs.getString(3));
				vo.setSexo(rs.getString(4));
				vo.setTipodocumento(rs.getString(5));
				vo.setNrodocumento(rs.getString(6));
				vo.setCorreo(rs.getString(7));
				vo.setCelular(rs.getString(8));
				vo.setClave(rs.getString(9));
				vo.setPerfil(rs.getString(10));
				vo.setCodprofesor(rs.getInt(11));
				vo.setFoto(rs.getString(12));
				return vo;
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Profesor insertar(Profesor pr) throws DAOExcepcion {
		String query = "insert into profesor(nombres,apepaterno,apematerno,sexo,tipodocumento,nrodocumento,correo,celular,"
				+ "clave,perfil,foto) values(?,?,?,?,?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, pr.getNombres());
			stmt.setString(2, pr.getApepaterno());
			stmt.setString(3, pr.getApematerno());
			stmt.setString(4, pr.getSexo());
			stmt.setString(5, pr.getTipodocumento());
			stmt.setString(6, pr.getNrodocumento());
			stmt.setString(7, pr.getCorreo());
			stmt.setString(8, pr.getCelular());
			stmt.setString(9, pr.getClave());
			stmt.setString(10, pr.getPerfil());
			stmt.setString(11, pr.getFoto());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("Error insertar");
			}

			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			pr.setCodprofesor(id);

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return pr;
	}

	public Profesor actualizar(Profesor pr) throws DAOExcepcion {
		String query = "update profesor set nombres=?, apepaterno=?,sexo=?,foto=?,celular=?,perfil=?,"
				+ "tipodocumento=?,apematerno=? where codprofesor=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, pr.getNombres());
			stmt.setString(2, pr.getApepaterno());
			stmt.setString(3, pr.getSexo());
			stmt.setString(4, pr.getFoto());
			stmt.setString(5, pr.getCelular());
			stmt.setString(6, pr.getPerfil());
			stmt.setString(7, pr.getTipodocumento());
			stmt.setString(8, pr.getApematerno());			
			stmt.setInt(9, pr.getCodprofesor());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return pr;
	}

	public Collection<Profesor> listar() throws DAOExcepcion {
		Collection<Profesor> c = new ArrayList<Profesor>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			String query = "select codProfesor,nombres,apepaterno,apematerno from profesor order by codProfesor";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Profesor es = new Profesor();
				es.setCodprofesor(rs.getInt("codProfesor"));
				es.setNombres(rs.getString("nombres"));
				es.setApepaterno(rs.getString("apepaterno"));
				es.setApematerno(rs.getString("apematerno"));
				c.add(es);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	public Collection<SubCategoria> listarCategoria(int id) throws DAOExcepcion {
		Collection<SubCategoria> c = new ArrayList<SubCategoria>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			String query = "select subcategoria_codcategoria,sc.nombre from categoria_profesor cp inner join subcategoria "
					+ " sc on cp.SubCategoria_codCategoria=sc.codSubcategoria where Profesor_codProfesor=?";
			stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			rs = stmt.executeQuery();
			while (rs.next()) {
				SubCategoria es = new SubCategoria();
				es.setCodsubcategoria(rs.getInt(1));
				es.setNombre(rs.getString(2));
				c.add(es);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

}
